var validateModule = (function() {
	var strategy = {};
	var validate = {};
	var my = {};
	var text = {};

	my.addStrategy = function(strate, func) {
		strategy[strate] = func;
	}

	my.addValidate = function(key, strate, err) {
		if (err) {
			(text[key] = !!text[key] ? text[key] : [])[strate] = err;
		}
		(validate[key] = !!validate[key] ? validate[key] : [])[strate] = strate;
	}

	my.removeValidate = function(key, strate) {
		if (validate[key])
			delete validate[key];
	}

	my.done = function(form, key) {
		var txt = [];
		if (validate[key]) {
			for (var i in validate[key]) {
				var valText = text && text[key] && text[key][i] || "";
				if (strategy[validate[key][i]](form[key], valText)) {
					txt.push(strategy[validate[key][i]](form[key], valText));
				}
			}
		}
		return txt;
	}
	return my;
})();
var baseStrategy = {
	getStrategy: function(config) {
		var strategy = config.strategy;
		var text = config.text;
		var value = config.value;
		var strategyKey;
		var tmp = "";
		for (var i = 0; i < strategy.length; i++) {
			strategyKey = strategy[i];
			tmp = this[strategyKey](value);
			if (tmp) {
				return tmp.replace("%s%", text);
			}
		}
	},
	isEmpty: function(val) {
		if (val == "") {
			return '请输入%s%';
		}
	},
	hasSpace: function(val) {
		if (/\s/.test(val)) {
			return '%s%不能包含空格';
		}
	},
	notCharacterStart: function(val) {
		if (!/^[a-zA-Z]/.test(val)) {
			return '%s%必须以字母开头';
		}
	},
	hasChinese: function(val) {
		if (/[\u4e00-\u9fa5]/.test(val)) {
			return '%s%不能包含中文';
		}
	},
	notChinese: function(val) {
		if (/[^\u4e00-\u9fa5]/.test(val)) {
			return '%s%必须是中文';
		}
	},
	hasChineseChar: function(val) {
		if (/[^\x00-\xff]/.test(val)) {
			return '%s%不能包含中文字符';
		}
	},
	hasSpecialChar: function(val) {
		if (/[`~!@#\$%\^\&\-\*\(\)\+<>\?:"\{\},\.\\\/;'\[\]]/.test(val)) {
			return '%s%不能包含特殊字符';
		}
		if (/[^\x00-\xff]/.test(val) && !/[\u4e00-\u9fa5]/.test(val)) {
			return '%s%不能包含特殊字符';
		}
	},
	more20: function(val) {
		if (val.length > 20) {
			return '%s%长度不大于20位字符';
		}
	},
	more10: function(val) {
		if (val.length > 10) {
			return '%s%长度不大于10位字符';
		}
	},
	less2: function(val) {
		if (val.length < 2) {
			return '%s%长度不少于2位字符';
		}
	},
	less4: function(val) {
		if (val.length < 4) {
			return '%s%长度不少于4位字符';
		}
	},
	less6: function(val) {
		if (val.length < 6) {
			return '%s%长度不少于6位字符';
		}
	},
	phoneLengthIllegal: function(val) {
		if (val.length != 11) {
			return '手机号长度不合法'
		}
	},
	notNumber: function(val) {
		if (!/^[0-9]*$/.test(val)) {
			return '%s%需要数字';
		}
	},
	not1Start: function(val) {
		if (!/^1/.test(val)) {
			return '手机号不合法'
		}
	},
	areaCode: function(val) {
		var areaCode = [11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 66, 71, 81, 91];
		var valCode = parseFloat(val.slice(0, 2));
		if (areaCode.indexOf(valCode) == -1) {
			return '请输入合法身份证号';
		}
	},
	notNumXx: function(val) {
		if (/[^0-9xX]/.test(val)) {
			return "请输入合法身份证号";
		}
	},
	cardNumberDateFail: function(val) {
		var year, month, day;
		if (val.length == 15) {
			year = "19" + val.slice(6, 8);
			month = val.slice(8, 10);
			day = val.slice(10, 12);
		}
		if (val.length == 18) {
			year = val.slice(6, 10);
			month = val.slice(10, 12);
			day = val.slice(12, 14);
		}
		var date = new Date(year + "-" + month + "-" + day);
		if (date == "Invalid Date") {
			return "请输入合法身份证号";
		}
		// chrome浏览器对时间的判断有进一
		if (date.getMonth() + 1 != month) {
			return "请输入合法身份证号";
		}
	},
	cardLength: function(val) {
		if (val.length != 15 && val.length != 18) {
			return "请输入合法身份证号";
		}
	},
	cardNumberLastVal: function(val) {
		var value = val.replace("x", "X");
		var key1 = value.split('');
		var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
		//校验位
		var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
		var sum = 0;
		var ai = 0;
		var wi = 0;
		for (var i = 0; i < 17; i++)
		{
			ai = key1[i];
			wi = factor[i];
			sum += ai * wi;
		}
		var last = parity[sum % 11];
		if (parity[sum % 11] != key1[17]) {
			return "身份证不合法";
		}
	}
};
validateModule.addStrategy("password", function(value, text) {
	var strategy = ["isEmpty", "hasSpace", "less6", "more20", "hasChinese"];
	var result = baseStrategy.getStrategy({
		strategy: strategy,
		value: value,
		text: "密码",
	})
	if (result) {
		return result;
	}
	return "";
});
validateModule.addStrategy("card_number", function(value, text) {
	var strategy = ["isEmpty", "hasSpace", "cardLength", "notNumXx", "areaCode", "cardNumberDateFail", "cardNumberLastVal"];
	var result = baseStrategy.getStrategy({
		strategy: strategy,
		value: value,
		text: "身份证号",
	})
	if (result) {
		return text || result;
	}
	return "";
});
validateModule.addStrategy("phone", function(value, text) {
	var strategy = ["isEmpty", "hasSpace", "phoneLengthIllegal", "not1Start", "notNumber"];
	var result = baseStrategy.getStrategy({
		strategy: strategy,
		value: value,
		text: "手机号",
	});
	if (result) {
		return text || result;
	}
	return "";
});
validateModule.addStrategy("user_name", function(value, text) {
	var strategy = ["isEmpty", "hasSpace", "less4", "more20", "notCharacterStart", "hasChinese", "hasSpecialChar"];
	var result = baseStrategy.getStrategy({
		strategy: strategy,
		value: value,
		text: "用户名",
	});
	if (result) {
		return text || result;
	}
	return "";
});
validateModule.addStrategy("name", function(value, text) {
	var strategy = ["isEmpty", "less2", "more10", "hasSpace", "notChinese"];
	var result = baseStrategy.getStrategy({
		strategy: strategy,
		value: value,
		text: "姓名",
	});
	if (result) {
		return text || result;
	}
	return "";
});
